package com.hyjx.framework.component.interfaceservice.datauser;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.component.codetable.CodeTableManager;
import com.hyjx.framework.component.interfaceservice.InterFaceConstants;
import com.hyjx.framework.component.mutidatasource.DynamicDataSource;
import com.hyjx.framework.orm.dao.Pager;

/**
 * 数据服务用户控制器
 */
@Controller
@RequestMapping("/datauser")
public class  DataUserController{
	@Resource
	private DataUserService service;
	

	/**
	 * 到amq测试页面
	 * @return
	 */
	@RequestMapping(value = "/amq" , method = RequestMethod.GET)
	public String amq(HttpServletRequest request){
		return "/framework/interfaceservice/datauser/datauser_amq";
	}
	
	/**
	 * 查询列表数
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/amqlist",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String amqlist(HttpServletRequest request){
		
		DynamicDataSource.setCustomerType("activemq");
		List list =service.queryListAmq();
		DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE);
		for(int i=0;i<list.size();i++){
			amq a=(amq) list.get(i);
			String c=a.getCONTAINER();
			String m=a.getMSGID_PROD();
			byte[] b=a.getMSG();
			ByteArrayInputStream in=new ByteArrayInputStream(b);
			InputStream inputStream=in;
			try {
				String temp3 = new String(b, "UTF-8");
				System.out.println("temp3"+temp3);
				String by=new String(b);
				String b1="";
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
		/*for(int i=0;i<li.getResultList().size();i++){
			DataUserModel temp=(DataUserModel) li.getResultList().get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_user_type", temp.getUsertype());
			temp.setUsertype(codesrvname);
		}*/
		/*StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"total\": "+li.size()+",");
		sb.append("\"rows\": ");
		sb.append(CommonUtil.getJsonString(li).toString());
		sb.append("}");*/
		
	
		return "";
	//	return service.queryList(model);
	}
	
	/**
	 * 到列表页面
	 * @return
	 */
	@RequestMapping(value = "/gotoList" , method = RequestMethod.GET)
	public String gotoList(HttpServletRequest request){
		return "/framework/interfaceservice/datauser/datauser_list";
	}
	/**
	 * 查询列表数据
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryList" , method = RequestMethod.POST)
	@ResponseBody
	public String queryList(DataUserModel model,HttpServletRequest request){
		Pager li=service.queryListA(model);
		for(int i=0;i<li.getResultList().size();i++){
			DataUserModel temp=(DataUserModel) li.getResultList().get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_user_type", temp.getUsertype());
			temp.setUsertype(codesrvname);
		}
		/*StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"total\": "+li.size()+",");
		sb.append("\"rows\": ");
		sb.append(CommonUtil.getJsonString(li).toString());
		sb.append("}");*/
		
	
		return CommonUtil.getPageJsonStr(li);
	//	return service.queryList(model);
	}
	/**
	 * 到新增页面
	 * @return
	 */
	@RequestMapping(value = "/gotoAdd",  method = {RequestMethod.GET,RequestMethod.POST})
	public String gotoAdd(HttpServletRequest request) {
		/*DynamicDataSource.setCustomerType("ds_1");
		String s=DynamicDataSource.getCustomerType();
		service.testMultiDs();
		DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE);*/
		
		return "/framework/interfaceservice/datauser/datauser_add";
	}
	/**
	 * 保存对象
	 * @param DataUserModel
	 * @return
	 */
	@RequestMapping(value = "/doAdd", method = RequestMethod.POST)
	@ResponseBody
	public String doAdd(DataUserModel model) {
		model.setUserid(StringHelper.getPrimaryKey());
		if(service.add(model)){
			//这里要清空SysConstants里面的userinfo的缓存
			InterFaceConstants.setUserConfig();
			InterFaceConstants.getUserConfig();
			return "success";
		}else{
			return "failure";
		}
	}
	/**
	 * 到修改页面
	 * request
	 * @return
	 */
	@RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView gotoEdit(HttpServletRequest request) {
		String id = request.getParameter("id");
		
		DataUserModel qp=new DataUserModel();
		
		qp.setUserid(id);
		DataUserModel model =(DataUserModel) (service.getInfoById(qp));
		
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/datauser/datauser_edit");
		mav.addObject("model",model);
		return mav;
	}
	/**
	 * 更新数据
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/doEdit", method = RequestMethod.POST)
	@ResponseBody
	public String doEdit(DataUserModel model) {
		if(service.edit(model)>0){
			return "success";
		}else{
			return "failure";
		}
	}
	/**
	 * 删除
	 * request
	 * @return
	 */
	@RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
	@ResponseBody
	public String gotoDelete(HttpServletRequest request) {
		String id = request.getParameter("ids");
		
		DataUserModel qp=new DataUserModel();
		qp.setUserid(id);
		
		if(service.delete(qp)>0){
			return "success";
		}else{
			return "failure";
		}
	}
	/**
	 * 到查看页面
	 * request
	 * @return
	 */
	@RequestMapping(value = "/gotoView", method = RequestMethod.GET)
	public ModelAndView gotoView(HttpServletRequest request) {
		String id = request.getParameter("id");
		DataUserModel qp=new DataUserModel();
		qp.setUserid(id);
		DataUserModel model =(DataUserModel) (service.getInfoById(qp));
		
		String usertypename=CodeTableManager.getNameByCode("interface_user_type", model.getUsertype());
		model.setUsertype(usertypename);
		
		if(model.getValidity().equals("0")){
			model.setValidity("无效");
		}else{
			model.setValidity("有效");
		}
		//String user=CodeTableManager.getNameByCode("interface_user_type", model.getUsertype());
		ModelAndView mav = new ModelAndView("/framework/interfaceservice/datauser/datauser_view");
		mav.addObject("model",model);
		return mav;
	}
	
	
	
	/**
	 * 确认用户登录名是否重复
	 * 
	 * @param userlogname
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/userConfirm")
	@ResponseBody
	public String userConfirm(String userlogname,String userid) {
		DataUserModel du=new DataUserModel();
		du.setUserlogname(userlogname);
		if(userid!=null){
			du.setUserid(userid);
		}
		
		int count=service.userNameLimit(du);
		if(count>0){
			return "sameuserlogname";
		}else{
			return "success";
		}

	}
}